@extends('manage._layout')

@section('page-title')
    添加菜单
@stop
@section('page-breadcrumbs')
    <li>
        <a href="{{route('menu_list')}}" title="">
            菜单管理
        </a>
    </li>
    <li class="current">
        <a href="javascript:;" title="">
            添加
        </a>
    </li>
@stop
@section('page-buttons')
    <li>
        <a href="{{route('menu_list')}}" title="">
            <i class="icon-reply"></i>
            <span>返回</span>
        </a>
    </li>
@stop

@section('page-content')
    <form class="form-horizontal" role="form" action="" enctype="multipart/form-data">
        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">菜单名称</label>
            <div class="col-sm-5">
                <input type="text" class="form-control" name="name" id="name"
                       placeholder="请输入菜单名称">
            </div>
        </div>

        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">链接地址</label>
            <div class="col-sm-5">
                <input type="text" class="form-control" name="url" id="url"
                       placeholder="请输入链接地址"><span style="color:red">填写路由别名(如menu_list)</span>
            </div>
        </div>


        <div class="form-group">
            <label for="client" class="col-sm-2 control-label">所属一级菜单</label>
            <div class="col-sm-2">
                <select name="first_menu" id='first_menu' class="form-control">
                    @foreach($firstMenuData as $k=>$val)
                    <option value="{{$val->id}}">{{$val->name}}</option>
                    @endforeach
                </select>
            </div>
        </div>

        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">排序</label>
            <div class="col-sm-5">
                <input type="text" class="form-control" name="sort" id="sort"
                       placeholder="请输入排序">
            </div>
        </div>


        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="button" class="btn btn-default" id="sub">添加</button>
            </div>
        </div>
    </form>
    <script>
        $(document).ready(function () {
            $('#sub').click(function () {
                if ($('#name').val() == '') {
                    layer.alert('请填写菜单名称');
                    return false;
                }
                if ($('#url').val() == '') {
                    layer.alert('请填写链接地址');
                    return false;
                }

                var postdata = {
                    name: $('#name').val(),
                    url:$('#url').val(),
                    sort: $('#sort').val(),
                    first_menu:$('#first_menu').val()
                };

                $.ajax({
                    type: 'POST',
                    url: '{{URL::route('menu_add_post')}}',
                    headers: { 'X-CSRF-TOKEN' : '{{ csrf_token() }}' },
                    data: postdata,
                    success: function (res) {
                        if (res.error == 0) {
                            location.href = '{{URL::route('menu_list')}}';
                        } else {
                            layer.alert(res.message);
                        }
                    },
                    error: function(XMLHttpRequest) {
                        if(XMLHttpRequest.status==403){
                            layer.alert(noAccessMsg);
                        }else{
                            layer.alert(otherErrorMsg);
                        }
                    }
                });
            });
            //////////////////////////


        });
    </script>
@stop